<?php
require_once('includes/connexion_bdd.php');
require_once('includes/fonctions_donnees.php');

class photo  {

	protected $id;
	protected $idCreation;
	protected $nom;
	protected $erreurs;
	protected $valid;

	public function __construct(){

		$num_args = func_num_args();

		switch($num_args){
			case 0 :
				$bool =true;
				break;
			case 2 :
				$bool =$this->setIdCreation(func_get_arg(0));
				$bool &= $this->setNom(func_get_arg(1));
				break;
			default :
				$bool=false;
			$this->erreurs.="Param&egrave;tres de construction photo invalides! <br />";
			break;
		}
		$this->valid = $bool ;

	}

	public function getIdCreation(){
		return affichage($this->idCreation);
	}

	public function setIdCreation($idCreation){
		if(!empty($idCreation) && is_numeric($idCreation)){
			$this->idCreation = clean_data($idCreation);
			return 1;
		}
		else {
			$this->erreurs.="La cr&eacute;ation est introuvable <br />";
			return 0;
		}
	}
	public function getNom(){
		return affichage($this->nom);
	}

	public function setNom($nom){
		if(!empty($nom)){
			$this->nom = clean_data($nom);
			return 1;
		}
		else {
			$this->erreurs.="Le nom est obligatoire <br />";
			return 0;
		}
	}
	public function isValid(){
		return $this->valid;
	}

	public function getErreurs(){
		return $this->erreurs;
	}


	public function ajoutBD() {

		try {
			$bdd = Connect();

			$req = $bdd->prepare('INSERT INTO photo( idCreation, nom)
									VALUES(:idCreation, :nom)');
			$req->execute(array('idCreation' => $this->idCreation,
								'nom' => $this->nom));

			$this->id = $bdd->lastInsertId();
			return $this->id;
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}

	public function modifBD($id) {

		try {
			$bdd = Connect();

			$req = $bdd->prepare('UPDATE photo SET idCreation = :idCreation, nom = :nom
								WHERE id=:id');
			$req->execute(array('idCreation' => $this->idCreation,
								'nom' => $this->nom,
								'id' => $id));
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}


	public static function supprBD($id) {

		try {
			$bdd = Connect();
			$req = $bdd->prepare('DELETE FROM photo WHERE id=?');
			$req->execute(array($id));
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}
	
	public function getPhotoByCreation($idCreation){
		
		try {
			$bdd = Connect();
			$req = $bdd->prepare('SELECT *	FROM  photo p
														WHERE idCreation = :idCreation
											');		
			$req->execute(array('idCreation' => $idCreation));
			return $req;
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}

}
?>